//
//  LeadViewController.m
//  
//
//  Created by dllo on 15/10/19.
//
//

#import "LeadViewController.h"



@interface LeadViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, assign) CGFloat mark;
@property (nonatomic, retain) UIScrollView *scroller;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UIImageView *imageV_1;
@property (nonatomic, retain) UILabel *label;
@end

@implementation LeadViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.scroller = [[UIScrollView alloc] initWithFrame:self.view.frame];
    self.scroller.contentSize = CGSizeMake(self.view.frame.size.height * 1200 / 675, self.view.frame.size.height);
    self.scroller.bounces = NO;
    self.scroller.contentOffset= CGPointMake(self.view.frame.size.height * 1200 / 675 - self.view.frame.size.width, 0) ;
    [self.view addSubview:self.scroller];
    
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height * 1200 / 675, self.view.frame.size.height)];
    image.image = [UIImage imageNamed:@"150113122411-1.jpg"];
    [self.scroller addSubview:image];
    
    
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 100, self.view.frame.size.height / 4  - 100*HEIGHT, 200*WIDTH, 200*WIDTH)];
    
    self.imageV.image = [UIImage imageNamed:@"图标.png"];
    self.imageV.alpha = 0;
    [self.view addSubview:self.imageV];
    
    
    self.imageV_1 = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40 * WIDTH, self.view.frame.size.height / 4 * 3  , 80 * WIDTH, 80 * WIDTH)];
    
    self.imageV_1.backgroundColor = [UIColor whiteColor];
    self.imageV_1.alpha = 0;
    self.imageV_1.layer.cornerRadius = self.imageV_1.frame.size.width / 2;
    self.imageV_1.layer.masksToBounds = YES;
    [self.view addSubview:self.imageV_1];
    
    
    
    
    
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40 * WIDTH, self.view.frame.size.height / 4 * 3  , 80 * WIDTH, 80 * WIDTH)];
    self.label.text = @"Let's go";
    self.label.alpha = 0;
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.font = [UIFont systemFontOfSize:16 weight:1];
    self.label.textColor = [UIColor blackColor];
    [self.view addSubview:self.label];
    
    
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent)];
    self.imageV_1.userInteractionEnabled = YES;
    [self.imageV_1 addGestureRecognizer:singleFingerOne];
    
    
    
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeACtion) userInfo:nil repeats:YES];
    self.mark = 0;
}

- (void)timeACtion{
    
    if (self.mark > 50 && self.mark < 250) {
        self.imageV.alpha = (CGFloat)((self.mark - 50) / 200);
        NSLog(@"232323.....%f", (self.mark - 250) / 100);
    }
    
    if (self.mark >= 250) {
        self.scroller.scrollEnabled = NO;
        self.imageV_1.alpha = 0.4;
        self.label.alpha = 0.7;
    } else {
        self.scroller.contentOffset = CGPointMake((self.view.frame.size.height * 1200 / 675 - self.view.frame.size.width) - (self.view.frame.size.height * 1200 / 675 - self.view.frame.size.width) / 250 * self.mark, 0);
    }
       self.mark++;

    
    
}

- (void)handleSingleFingerEvent{
    NSUserDefaults *visit = [NSUserDefaults standardUserDefaults];
    [visit setObject:@"yes" forKey:@"firstvisit"];
    
    [visit synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"lead" object:nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row < 4) {
        
        LeadFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LeadFirst" forIndexPath:indexPath];
        
        NSMutableArray *imageArr = [NSMutableArray arrayWithObjects:@"leadImage1.jpeg", @"leadImage2.jpeg", @"leadImage3.jpeg", @"leadImage4.jpg", nil];

        NSMutableArray *numArr = [NSMutableArray arrayWithObjects:@"5", @"4", @"3", @"2", nil];
        
        cell.photoStr = [imageArr objectAtIndex:indexPath.row];
        
        cell.numStr = [numArr objectAtIndex:indexPath.row];
        
        [cell setValue];
        
        return cell;
        
    } else {
        
        LeadSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LeadSecond" forIndexPath:indexPath];
        
        [cell setValue];
        
        return cell;
        
    }
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
